﻿using System;
using System.Collections.Generic;
using System.Data;
using FluentData;
using AdverseEventReportSystem.Core;
namespace AdverseEventReportSystem.Repository {
    public class BaseRepository<T> where T : class {
        /// <summary>
        /// 返回单个实体
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual T GetQuerySingle(string sqlStr, IDbContext context = null, params Object[] objects) {


            if (context == null) context = Db.GetInstance().Context();
            T obj = context.Sql(sqlStr, objects).QuerySingle<T>();
            return obj;
        }
        /// <summary>
        /// 返回实体列表
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual List<T> GetQueryMany(string sqlStr, IDbContext context = null, params Object[] objects) {
            if (context == null) context = Db.GetInstance().Context();
            List<T> objlist = context.Sql(sqlStr, objects).QueryMany<T>();
            return objlist;
        }
        /// <summary>
        /// 返回DataTable
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual DataTable GetDataTable(string sqlStr, IDbContext context = null, params Object[] objects) {
            if (context == null) context = Db.GetInstance().Context();
            DataTable dt = context.Sql(sqlStr, objects).QuerySingle<DataTable>();
            return dt;
        }
        /// <summary>
        /// 返回条数
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual int GetCount(string sqlStr, IDbContext context = null, params Object[] objects) {
            if (context == null) context = Db.GetInstance().Context();
            int count = context.Sql(sqlStr, objects).QuerySingle<int>();
            return count;
        }
        /// <summary>
        /// 获取单个字段的值 
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <param name="context"></param>
        /// <param name="objects"></param>
        /// <returns></returns>
        public virtual string Getobject(string sqlStr, IDbContext context = null, params Object[] objects) {
            if (context == null) context = Db.GetInstance().Context();
            string count = context.Sql(sqlStr, objects).QuerySingle<string>();
            return count;
        }
        /// <summary>
        /// 更新操作
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual int Update(string sqlStr, IDbContext context = null, params Object[] objects) {
            if (context == null) context = Db.GetInstance().Context();
            int rowsAffected = context.Sql(sqlStr, objects).Execute();
            return rowsAffected;
        }
        /// <summary>
        /// 删除操作
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual int Del(string sqlStr, IDbContext context = null, params Object[] objects) {
            if (context == null) context = Db.GetInstance().Context();
            int rowsAffected = context.Sql(sqlStr, objects)
                    .Execute();
            return rowsAffected;
        }
        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="data"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public List<T> GetQueryManyForPage(SelectBuilder data, out int count, IDbContext context = null, params Object[] objects) {
            if (context == null) context = Db.GetInstance().Context();
            string sqlStr = Db.GetInstance().GetSqlForSelectBuilder(data);
            string sqlStrCount = Db.GetInstance().GetSqlForTotalBuilder(data);
            count = context.Sql(sqlStrCount, objects).QuerySingle<int>();
            List<T> objlist = context.Sql(sqlStr, objects).QueryMany<T>();
            return objlist;
        }
        /// <summary>
        /// 返回DataTable
        /// </summary>
        /// <param name="data"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public DataTable GetDataTableForPage(SelectBuilder data, out int count, IDbContext context = null, params Object[] objects) {
            if (context == null) context = Db.GetInstance().Context();
            string sqlStr = Db.GetInstance().GetSqlForSelectBuilder(data);
            string sqlStrCount = Db.GetInstance().GetSqlForTotalBuilder(data);
            count = context.Sql(sqlStrCount, objects).QuerySingle<int>();
            return GetDataTable(sqlStr, context, objects);
        }
        /// <summary>
        /// 分页列表存储过程（不带GROUP BY）
        /// </summary>
        /// <param name="data"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public DataTable GetQueryManyForPage_Proc(SelectBuilder data, out int count, IDbContext context = null, params Object[] objects)
        {
            if (context == null) context = Db.GetInstance().Context();
            if (data.WhereSql != "") data.WhereSql = " where " + data.WhereSql;
            var store = context.StoredProcedure("sp_Sql_Paging")
                                        .ParameterOut("recordCount", DataTypes.Int32)
                                        .Parameter("SqlDataTable", data.From)
                                        .Parameter("Fields", data.Select)
                                        .Parameter("pageIndex", data.PagingCurrentPage)
                                        .Parameter("pageSize", data.PagingItemsPerPage)
                                        .Parameter("strOrderBy", data.OrderBy)
                                        .Parameter("strWhere", data.WhereSql);


            DataTable result = store.QuerySingle<DataTable>();
            count = store.ParameterValue<int>("recordCount");
            return result;
        }

        /// <summary>
        /// 自定义查询返回DataTable
        /// </summary>
        /// <param name="data"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public DataTable GetDataTableSelectBuilder(SelectBuilder data, IDbContext context = null, params Object[] objects)
        {
            if (context == null) context = Db.GetInstance().Context();
            string sqlStr = Db.GetInstance().GetSelectSql(data);
            return GetDataTable(sqlStr, context, objects);
        }
    }
}